//
//  TestAlertViewController.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2023/11/1.
//  Copyright © 2023 moon. All rights reserved.
//

#import "TestAlertViewController.h"
#import "Menu.h"
#import "ListViewController.h"
#import "MKUIElementListViewCellSelection+Test.h"
#import "MKUIElementListViewCellSwitch+Test.h"
#import "MKUITestCache.h"
#import "TestMKUIElementViewUIAlertAction.h"
#import "TestMKUIElementViewMKUIAlertAction.h"
#import "MKUIElementListViewCellButton+Test.h"
#import "YYText.h"
#import "NSString+Test.h"
#import "TestElementHeaders.h"
@interface TestUIAlertSheetView : MKUIAlertView

@end
@implementation TestUIAlertSheetView
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.preferredStyle = MKUIAlertViewStyleActionSheet;
        self.backgroundColor = [UIColor mk_colorWithLight:UIColor.whiteColor];
        self.contentView.groupBackgroundColor = [UIColor mk_colorWithLight:UIColor.whiteColor];
        self.contentView.groupBackgroundViewClass = nil;
        self.contentView.contentView.backgroundColor = [UIColor mk_colorWithLight:[UIColor mk_colorWithString:@"#F7F8FA"]];
        self.contentView.sectionInteritemSpacing = 10;
        self.contentView.groupCornerRadius = 0;
        self.contentView.groupInsets = UIEdgeInsetsZero;
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect f1 = self.bounds;
    CGFloat groupCornerRadius = 16;
    UIBezierPath *path = [[UIBezierPath alloc] init];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    MKCGAxis Y = MKCGAxisY;
    if(self.preferredStyle==MKUIAlertViewStyleActionSheet){
        MKCGRectAddLength(&f1, Y, groupCornerRadius);
    }else{
        UIEdgeInsets insets = self.contentView.contentInset;
        MKCGRectAddLength(&f1, Y, -insets.bottom);
    }
    [path appendPath:[UIBezierPath bezierPathWithRoundedRect:f1 cornerRadius:groupCornerRadius]];
    maskLayer.path = path.CGPath;
    self.layer.mask = maskLayer;
}
@end

@interface TestMKUIAlertAction_Close()
@property(nonatomic,strong) NSTimer *timer;
@property(nonatomic,strong) NSDate *beginDate;
@property(nonatomic,strong,nullable) NSString *originTitle;
@end
@implementation TestMKUIAlertAction_Close
- (id)init{
    if(self=[super init]){
        self.willDisplayAction = ^(TestMKUIAlertAction_Close * _Nonnull action) {
            [action beginTimer];
        };
    }
    return self;
}
- (void)beginTimer{
    if(self.timer)return;
    [self.timer invalidate];
    if(self.duration<=0)return;
    @MK_WEAKIFY(self);
    self.beginDate = [NSDate date];
    self.originTitle = self.title;
    self.timer = [NSTimer mk_scheduledTimerWithTimeInterval:1/30.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
        @MK_NORMALIZE(self);
        NSTimeInterval duration = [NSDate.date timeIntervalSinceDate:self.beginDate];
        [self reloadWithTime:duration];
        [self.actionCell reloadAction];
    }];
    [self reloadWithTime:0];
}
- (void)reloadWithTime:(NSTimeInterval)time{
    if(self.titleBlock){
        self.title = self.titleBlock(self,self.originTitle,time);
    }else{
        NSInteger left = ceil(self.duration-time);
        self.title = left<=0?self.originTitle:[NSString stringWithFormat:@"%@(%ld)",self.originTitle,left];
    }
    self.enabled = time>=self.duration;
    if(time>=self.duration){
        [self.timer invalidate];
        self.timer = nil;
        if(self.whenTimerFinish){
            self.whenTimerFinish(self);
        }
    }
}
- (void)endTimer{
    [self.timer invalidate];
    self.timer = nil;
}
- (void)dealloc{
    [self.timer invalidate];
}
@end

@implementation TestMKUIAlertActionView_Button
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.enableSelection = NO;
        self.button = [[MKUIFlowLayoutButton alloc] init];
        self.button.layer.cornerRadius = 3;
        self.button.clipsToBounds = YES;
        [self.button addTarget:self action:@selector(_onButtonDidTap:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.contentView addSubview:self.button];
        self.titleLabel.hidden = YES;
    }
    return self;
}
- (void)_onButtonDidTap:(id)sender{
    [self handleAction];
}
- (void)layoutContentView{
    [super layoutContentView];
    CGRect bounds = UIEdgeInsetsInsetRect(self.contentView.bounds, UIEdgeInsetsMake(5, 10, 5, 10));
    self.button.frame = bounds;
}
- (void)reloadAction{
    [super reloadAction];
    self.enableSelection = NO;
    
    [self.button setTitle:self.action.title forState:(UIControlStateNormal)];
    UIColor *color = UIColor.whiteColor;
    UIColor *bgColor = nil;
    CGFloat fontSize = self.alertViewStyle==MKUIAlertViewStyleAlert?17:20;
    UIFont *font = nil;
    switch (self.action.style) {
        case MKUIAlertActionStyleDefault:
            bgColor = UIColor.mk_defaultActionStyleColor;
            font = [UIFont systemFontOfSize:fontSize];
            break;
        case MKUIAlertActionStyleCancel:
            bgColor = UIColor.mk_cancelStyleActionColor;
            font = [UIFont boldSystemFontOfSize:fontSize];
            break;
        case MKUIAlertActionStyleDestructive:
            bgColor = UIColor.mk_destructiveActionStyleColor;
            font = [UIFont systemFontOfSize:fontSize];
            break;
        default:
            color = UIColor.mk_defaultActionStyleColor;
            font = [UIFont systemFontOfSize:fontSize];
            break;
    }
    if(!self.action.isEnabled){
        color = UIColor.mk_disableActionColor;
    }
    self.button.titleLabel.font = font;
    [self.button setTitleColor:color forState:(UIControlStateNormal)];
    [self.button mk_setBackgroundColorForNormalAndHighlightedState:bgColor];
}
@end

@implementation TestMKUIAlertViewBackground
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.clipsToBounds = NO;
        self.bgView = [[UIView alloc] init];
        self.bgView.backgroundColor = UIColor.mk_alertBackgroundColor;
        [self addSubview:self.bgView];
        //
        self.iconView = [[UIImageView alloc] initWithImage:Menu.randomImage];
        self.iconView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:self.iconView];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    CGRect f1 = bounds;
    self.bgView.frame = f1;
    {
        CGFloat groupCornerRadius = 10;
        UIBezierPath *path = [[UIBezierPath alloc] init];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        CGRect f1 = bounds;
        MKCGAxis Y = MKCGAxisY;
        MKUIAlertView *alert = [self mk_firstSuperViewWithClass:MKUIAlertView.class];
        if(alert.preferredStyle==MKUIAlertViewStyleActionSheet){
            MKCGRectAddLength(&f1, Y, groupCornerRadius);
        }else{
            UIEdgeInsets insets = alert.contentView.contentInset;
            MKCGRectAddLength(&f1, Y, -insets.bottom);
        }
        [path appendPath:[UIBezierPath bezierPathWithRoundedRect:f1 cornerRadius:groupCornerRadius]];
        maskLayer.path = path.CGPath;
        self.bgView.layer.mask = maskLayer;
    }
    //
    CGRect f2 = self.iconView.frame;
    f2.size.width = f2.size.height = 100;
    MKCGRectAlignMidXToRect(&f2, bounds);
    MKCGRectSetMidY(&f2, 0);
    self.iconView.frame = f2;
}
@end

@interface TestMultipAlertParam()
@property(nonatomic,strong) NSString *subTitle;
@property(nonatomic,strong) NSString *headTitle;
@property(nonatomic,strong) NSString *footTitle;
@property(nonatomic,assign) NSTimeInterval closeDuration;
@end

@implementation TestMultipAlertParam
- (id)init{
    if(self=[super init]){
        self.title = @"各种弹窗效果";
        self.mk_customTabBarItem.image = Menu.randomImage;
    }
    return self;
}
- (void)viewDidLoad{
    [super viewDidLoad];
    self.listView.style = MKUIListViewStyleInsetGrouped;
    
    self.subTitle = [MKUITestCache.sharedStorage mk_stringForKeyPath:@"TestMultipAlertParam.subTitle"];
    self.headTitle = [MKUITestCache.sharedStorage mk_stringForKeyPath:@"TestMultipAlertParam.headTitle"];
    self.footTitle = [MKUITestCache.sharedStorage mk_stringForKeyPath:@"TestMultipAlertParam.footTitle"];
    self.closeDuration = [MKUITestCache.sharedStorage mk_CGFloatForKeyPath:@"TestMultipAlertParam.closeDuration"];
    [self reloadData];
}
- (void)reloadData{
    @MK_WEAKIFY(self);
    [self.listView deleteAllSections];
    [self.listView addSection];
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSStringElementWithTitle:@"headTitle" selected:self.headTitle whenDone:^(NSString * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            self.headTitle = selectedValue;
            [MKUITestCache.sharedStorage setObject:selectedValue forKey:@"TestMultipAlertParam.headTitle"];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSStringElementWithTitle:@"footTitle" selected:self.footTitle whenDone:^(NSString * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            self.footTitle = selectedValue;
            [MKUITestCache.sharedStorage setObject:selectedValue forKey:@"TestMultipAlertParam.footTitle"];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellButton *el = [MKUIElementListViewCellButton testButtonElementWithTitle:@"添加headTitle、footTitle(不参与滚动）" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
            @MK_NORMALIZE(self);
            MKUIAlertController *alert = [self genAlertController];
            if(self.headTitle){
                MKUIAlertViewTextCellView *titleView = [[MKUIAlertViewTextCellView alloc] init];
                titleView.contentInsets = UIEdgeInsetsMake(10, 10, 10, 10);
                titleView.textLabel.font = [UIFont systemFontOfSize:17];
                titleView.text = self.headTitle;
                alert.alertView.contentView.listHeaderView = titleView;
            }
            if(self.footTitle){
                MKUIAlertViewTextCellView *titleView = [[MKUIAlertViewTextCellView alloc] init];
                titleView.contentInsets = UIEdgeInsetsMake(10, 10, 10, 10);
                titleView.textLabel.font = [UIFont systemFontOfSize:17];
                titleView.text = self.footTitle;
                alert.alertView.contentView.listFooterView = titleView;
            }
            [self presentViewController:alert animated:YES completion:nil];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellButton *el = [MKUIElementListViewCellButton testButtonElementWithTitle:@"添加headTitle、footTitle到Section0(不参与滚动）" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
            @MK_NORMALIZE(self);
            MKUIAlertController *alert = [self genAlertController];
            if(self.headTitle){
                MKUIAlertViewTextCellView *titleView = [[MKUIAlertViewTextCellView alloc] init];
                titleView.textLabel.font = [UIFont systemFontOfSize:17];
                titleView.text = self.headTitle;
                [alert.alertView.headContentView addItem:titleView];
            }
            if(self.footTitle){
                MKUIAlertViewTextCellView *titleView = [[MKUIAlertViewTextCellView alloc] init];
                titleView.textLabel.font = [UIFont systemFontOfSize:17];
                titleView.text = self.footTitle;
                [alert.alertView.footContentView addItem:titleView];
            }
            [self presentViewController:alert animated:YES completion:nil];
        }];
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSStringElementWithTitle:@"subTitle" selected:self.subTitle whenDone:^(NSString * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            self.subTitle = selectedValue;
            [MKUITestCache.sharedStorage setObject:selectedValue forKey:@"TestMultipAlertParam.subTitle"];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellButton *el = [MKUIElementListViewCellButton testButtonElementWithTitle:@"添加副标题subTitle" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
            @MK_NORMALIZE(self);
            MKUIAlertController *alert = [self genAlertController];
            if(self.subTitle){
                MKUIAlertViewTextCellView *titleView = [[MKUIAlertViewTextCellView alloc] init];
                titleView.textLabel.font = [UIFont systemFontOfSize:13];
                titleView.text = self.subTitle;
                titleView.textLabel.textColor = [UIColor mk_colorWithLight:UIColor.grayColor];
                [alert.alertView.bodyContentView setCellInteritemSpacing:6 forSection:0];
                [alert.alertView.bodyContentView addItem:titleView atSectionIndex:0];
            }
            [self presentViewController:alert animated:YES completion:nil];
        }];
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellButton *el = [MKUIElementListViewCellButton testButtonElementWithTitle:@"添加图片内容" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
            @MK_NORMALIZE(self);
            MKUIAlertController *alert = [self genAlertController];
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.layer.borderColor = UIColor.redColor.CGColor;
            imageView.layer.borderWidth = 1;
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            imageView.image = Menu.randomImage;
            [alert.alertView.bodyContentView addItem:imageView atSectionIndex:0];
            [self presentViewController:alert animated:YES completion:nil];
        }];
        [self.listView addItem:el];
    }
    //我同意并阅读《用户隐私条款》和《协议政策》
    [self.listView addSection];
    {
        MKUIElementListViewCellButton *el = [MKUIElementListViewCellButton testButtonElementWithTitle:@"添加富文本" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
            @MK_NORMALIZE(self);
            MKUIAlertController *alert = [self genAlertController];
            @MK_WEAKIFY(alert);
            YYLabel *privacyView = [[YYLabel alloc] init];
            privacyView.numberOfLines = 0;
            
            NSString *privacyString = @"我同意并阅读《用户隐私条款》和《协议政策》。我同意并阅读《用户隐私条款》和《协议政策》。我同意并阅读《用户隐私条款》和《协议政策》。我同意并阅读《用户隐私条款》和《协议政策》。我同意并阅读《用户隐私条款》和《协议政策》。我同意并阅读《用户隐私条款》和《协议政策》。我同意并阅读《用户隐私条款》和《协议政策》。";
            NSMutableAttributedString *privacyAttr = [[NSMutableAttributedString alloc] initWithString:privacyString];
            privacyAttr.yy_font = [UIFont systemFontOfSize:14];
            
            privacyAttr.yy_color = [[UIColor mk_colorWithLight:UIColor.blackColor] mk_resolvedColorWithTraitCollection:self.traitCollection];
            
            [privacyAttr test_setLinkString:@"《用户隐私条款》" url:@"用户隐私条款链接" tapAction:^(NSString * _Nonnull string, NSString * _Nullable url) {
                @MK_NORMALIZE(alert);
                NSLog(@"click %@,url:%@",string,url);
                [alert dismissViewControllerAnimated:YES completion:nil];
            }];
            [privacyAttr test_setLinkString:@"《协议政策》" url:@"协议政策链接" tapAction:^(NSString * _Nonnull string, NSString * _Nullable url) {
                @MK_NORMALIZE(alert);
                NSLog(@"click %@,url:%@",string,url);
                [alert dismissViewControllerAnimated:YES completion:nil];
            }];
            
            privacyView.attributedText = privacyAttr;
            
            [alert.alertView.bodyContentView addSection];
            [alert.alertView.bodyContentView addItem:privacyView];
            [self presentViewController:alert animated:YES completion:nil];
        }];
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSIntegerElementWithTitle:@"倒计时" selected:self.closeDuration whenDone:^(NSInteger selectedValue) {
            @MK_NORMALIZE(self);
            self.closeDuration = selectedValue;
            [MKUITestCache.sharedStorage setObject:@(selectedValue) forKey:@"TestMultipAlertParam.closeDuration"];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellButton *el = [MKUIElementListViewCellButton testButtonElementWithTitle:@"添加倒计时关闭按钮" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
            @MK_NORMALIZE(self);
            MKUIAlertController *alert = [self genAlertController];
            TestMKUIAlertAction_Close *action = [TestMKUIAlertAction_Close actionWithTitle:@"倒计时关闭" style:(MKUIAlertActionStyleCancel) handler:^(MKUIAlertAction * _Nonnull action) {
            }];
            action.duration = self.closeDuration;
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:nil];
            [action beginTimer];
        }];
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellButton *el = [MKUIElementListViewCellButton testButtonElementWithTitle:@"文字内容居左展示" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
            @MK_NORMALIZE(self);
            MKUIAlertController *alert = [self genAlertController];
            alert.alertView.titleView.textLabel.textAlignment = NSTextAlignmentLeft;
            alert.alertView.messageView.textLabel.textAlignment = NSTextAlignmentLeft;
            alert.alertView.actionContentView.separatorInsets = MKUIEdgeInsetsMakeSameEdge(10);
            [self presentViewController:alert animated:YES completion:nil];
        }];
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellButton *el = [MKUIElementListViewCellButton testButtonElementWithTitle:@"展示多个Alert" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
            @MK_NORMALIZE(self);
            MKUIAlertController *alert = [MKUIAlertController alertControllerWithTitle:@"这是第一个Alert" message:@"多个alert展示效果" preferredStyle:(MKUIAlertViewStyleAlert)];
            @MK_WEAKIFY(alert);
            [alert addAction:[MKUIAlertAction actionWithTitle:@"取消" style:(MKUIAlertActionStyleCancel) handler:nil]];
            MKUIAlertAction *nextAction = [MKUIAlertAction actionWithTitle:@"展示下一个" style:(MKUIAlertActionStyleDefault) handler:^(MKUIAlertAction * _Nonnull action) {
                @MK_NORMALIZE(alert);
                [alert setAlertView:[self genAlertView] animated:YES completion:nil];
            }];
            nextAction.shouldDismissWhenActionHandle = NO;
            [alert addAction:nextAction];
            [self presentViewController:alert animated:YES completion:nil];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellButton *el = [MKUIElementListViewCellButton testButtonElementWithTitle:@"当前Window同时弹多个Alert" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
            NSMutableArray<MKUIAlertController *> *alerts = [[NSMutableArray alloc] init];
            for(int i=0;i<3;i++){
                MKUIAlertController *alert = [MKUIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"这是第%@个Alert",@(i+1)] message:@"同时弹多个Alert" preferredStyle:(MKUIAlertViewStyleAlert)];
                [alerts addObject:alert];
                [alert addAction:[MKUIAlertAction actionWithTitle:@"确定" style:(MKUIAlertActionStyleDefault) handler:nil]];
                [alert addAction:[MKUIAlertAction actionWithTitle:@"取消所有弹窗" style:(MKUIAlertActionStyleCancel) handler:^(__kindof MKUIAlertAction * _Nonnull action) {
                    for(MKUIAlertController *alert in alerts){
                        [alert dismissFromWindowWithAnimated:YES completion:nil];
                    }
                }]];
                [alert presentInWindow:UIApplication.sharedApplication.keyWindow animated:YES completion:nil];
            }
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellButton *el = [MKUIElementListViewCellButton testButtonElementWithTitle:@"独立Window同时弹多个AlertController" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
            NSMutableArray<MKUIAlertController *> *alerts = [[NSMutableArray alloc] init];
            for(int i=0;i<3;i++){
                MKUIAlertController *alert = [MKUIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"这是第%@个Alert",@(i+1)] message:@"同时弹多个Alert" preferredStyle:(MKUIAlertViewStyleAlert)];
                [alerts addObject:alert];
                [alert addAction:[MKUIAlertAction actionWithTitle:@"确定" style:(MKUIAlertActionStyleDefault) handler:nil]];
                if(i>0)[alert addAction:[MKUIAlertAction actionWithTitle:@"展示前一个" style:(MKUIAlertActionStyleDefault) handler:^(__kindof MKUIAlertAction * _Nonnull action) {
                    if(i==0)return;
                    [MKUIAlertWindow.sharedInstance showAlertController:alerts[i-1] animated:YES completion:nil];
                }]];
                [alert addAction:[MKUIAlertAction actionWithTitle:@"取消所有弹窗" style:(MKUIAlertActionStyleCancel) handler:^(__kindof MKUIAlertAction * _Nonnull action) {
                    for(MKUIAlertController *alert in alerts){
                        [MKUIAlertWindow.sharedInstance hideAlertController:alert animated:YES completion:nil];
                    }
                }]];
                [MKUIAlertWindow.sharedInstance showAlertController:alert animated:YES completion:nil];
            }
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellButton *el = [MKUIElementListViewCellButton testButtonElementWithTitle:@"独立Window同时弹多个AlertView" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
            NSMutableArray<MKUIAlertView *> *alerts = [[NSMutableArray alloc] init];
            for(int i=0;i<3;i++){
                MKUIAlertView *alert = [MKUIAlertView alertViewWithTitle:[NSString stringWithFormat:@"这是第%@个Alert",@(i+1)] message:@"同时弹多个Alert" preferredStyle:(MKUIAlertViewStyleAlert)];
                [alerts addObject:alert];
                [alert addAction:[MKUIAlertAction actionWithTitle:@"确定" style:(MKUIAlertActionStyleDefault) handler:nil]];
                if(i>0)[alert addAction:[MKUIAlertAction actionWithTitle:@"展示前一个" style:(MKUIAlertActionStyleDefault) handler:^(__kindof MKUIAlertAction * _Nonnull action) {
                    if(i==0)return;
                    [MKUIAlertWindow.sharedInstance showAlertView:alerts[i-1] animated:YES completion:nil];
                }]];
                [alert addAction:[MKUIAlertAction actionWithTitle:@"取消所有弹窗" style:(MKUIAlertActionStyleCancel) handler:^(__kindof MKUIAlertAction * _Nonnull action) {
                    for(MKUIAlertView *alert in alerts){
                        [MKUIAlertWindow.sharedInstance hideAlertView:alert animated:YES completion:nil];
                    }
                }]];
                [MKUIAlertWindow.sharedInstance showAlertView:alert animated:YES completion:nil];
            }
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellButton *el = [MKUIElementListViewCellButton testButtonElementWithTitle:@"独立Window同时弹多个AlertView/AlertController" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
            BOOL isAlertingView = MKUIAlertWindow.sharedInstance.isAlertingView;
            NSMutableArray *alerts = [[NSMutableArray alloc] init];
            for(int i=0;i<6;i++){
                if(i%2==0){
                    MKUIAlertView *alert = [MKUIAlertView alertViewWithTitle:[NSString stringWithFormat:@"这是第%@个AlertView",@(i+1)] message:[NSString stringWithFormat:@"同时弹多个AlertView(%@)",@(isAlertingView)] preferredStyle:(MKUIAlertViewStyleAlert)];
                    [alerts addObject:alert];
                    [alert addAction:[MKUIAlertAction actionWithTitle:@"确定" style:(MKUIAlertActionStyleDefault) handler:nil]];
                    if(i>0)[alert addAction:[MKUIAlertAction actionWithTitle:@"展示前一个" style:(MKUIAlertActionStyleDefault) handler:^(__kindof MKUIAlertAction * _Nonnull action) {
                        if(i==0)return;
                        if([alerts[i-1] isKindOfClass:MKUIAlertView.class]){
                            [MKUIAlertWindow.sharedInstance showAlertView:alerts[i-1] animated:YES completion:nil];
                        }else{
                            [MKUIAlertWindow.sharedInstance showAlertController:alerts[i-1] animated:YES completion:nil];
                        }
                    }]];
                    [alert addAction:[MKUIAlertAction actionWithTitle:@"取消所有弹窗" style:(MKUIAlertActionStyleCancel) handler:^(__kindof MKUIAlertAction * _Nonnull action) {
                        for(id alert in alerts){
                            if([alert isKindOfClass:MKUIAlertView.class]){
                                [MKUIAlertWindow.sharedInstance hideAlertView:alert animated:YES completion:nil];
                            }else if([alert isKindOfClass:MKUIAlertController.class]){
//                                [MKUIAlertWindow.sharedInstance hideAlertController:alert animated:YES completion:nil];
                            }
                        }
                    }]];
                    [MKUIAlertWindow.sharedInstance showAlertView:alert animated:YES completion:nil];
                }else{
                    MKUIAlertController *alert = [MKUIAlertController alertControllerWithTitle:[NSString stringWithFormat:[NSString stringWithFormat:@"同时弹多个AlertView(%@)",@(MKUIAlertWindow.sharedInstance.isAlertingView)],@(i+1)] message:@"同时弹多个AlertController" preferredStyle:(MKUIAlertViewStyleAlert)];
                    [alerts addObject:alert];
                    [alert addAction:[MKUIAlertAction actionWithTitle:@"确定" style:(MKUIAlertActionStyleDefault) handler:nil]];
                    if(i>0)[alert addAction:[MKUIAlertAction actionWithTitle:@"展示前一个" style:(MKUIAlertActionStyleDefault) handler:^(__kindof MKUIAlertAction * _Nonnull action) {
                        if(i==0)return;
                        if([alerts[i-1] isKindOfClass:MKUIAlertView.class]){
                            [MKUIAlertWindow.sharedInstance showAlertView:alerts[i-1] animated:YES completion:nil];
                        }else{
                            [MKUIAlertWindow.sharedInstance showAlertController:alerts[i-1] animated:YES completion:nil];
                        }
                    }]];
                    [alert addAction:[MKUIAlertAction actionWithTitle:@"取消所有弹窗" style:(MKUIAlertActionStyleCancel) handler:^(__kindof MKUIAlertAction * _Nonnull action) {
                        for(id alert in alerts){
                            if([alert isKindOfClass:MKUIAlertView.class]){
//                                [MKUIAlertWindow.sharedInstance hideAlertView:alert animated:YES completion:nil];
                            }else if([alert isKindOfClass:MKUIAlertController.class]){
                                [MKUIAlertWindow.sharedInstance hideAlertController:alert animated:YES completion:nil];
                            }
                        }
                    }]];
                    [MKUIAlertWindow.sharedInstance showAlertController:alert animated:YES completion:nil];
                }
            }
        }];
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellButton *el = [MKUIElementListViewCellButton testButtonElementWithTitle:@"Alert内容动态变更" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
            @MK_NORMALIZE(self);
            MKUIAlertController *alert = [self genAlertController];
            @MK_WEAKIFY(alert);
            alert.actions = nil;
            [alert addAction:[MKUIAlertAction actionWithTitle:@"取消" style:(MKUIAlertActionStyleCancel) handler:nil]];
            MKUIAlertAction *nextAction = [MKUIAlertAction actionWithTitle:@"title/message变长" style:(MKUIAlertActionStyleDefault) handler:^(MKUIAlertAction * _Nonnull action) {
                @MK_NORMALIZE(alert);
                if(alert.title)alert.title = [NSString stringWithFormat:@"%@\n%@",alert.title,alert.title];
                if(alert.message)alert.message = [NSString stringWithFormat:@"%@\n%@",alert.message,alert.message];
            }];
            nextAction.shouldDismissWhenActionHandle = NO;
            [alert addAction:nextAction];
            
            MKUIAlertAction *removeAction = [MKUIAlertAction actionWithTitle:@"title/message变短" style:(MKUIAlertActionStyleDefault) handler:^(MKUIAlertAction * _Nonnull action) {
                @MK_NORMALIZE(alert);
                if(alert.title)alert.title = [alert.title substringToIndex:alert.title.length/2];
                if(alert.message)alert.message = [alert.message substringToIndex:alert.message.length/2];
            }];
            removeAction.shouldDismissWhenActionHandle = NO;
            [alert addAction:removeAction];
            [self presentViewController:alert animated:YES completion:nil];
        }];
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellButton *el = [MKUIElementListViewCellButton testButtonElementWithTitle:@"自定义按钮展示" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
            @MK_NORMALIZE(self);
            MKUIAlertController *alert = [self genAlertController];
            alert.alertView.actionCellClass = TestMKUIAlertActionView_Button.class;
            MKUIListViewSeparatorView *separatorViewBetweentTextAndActions = alert.alertView.separatorViewBetweentTextAndActions;
            {
                @MK_WEAKIFY(alert);
                MKUIAlertAction *action = [MKUIAlertAction actionWithTitle:@"删除其他按钮" style:(MKUIAlertActionStyleDestructive) handler:^(MKUIAlertAction * _Nonnull action) {
                    @MK_NORMALIZE(alert);
                    if(alert.actions.count>1){
                        alert.actions = @[action];
                    }else{
                        [alert dismissViewControllerAnimated:YES completion:nil];
                    }
                }];
                action.shouldDismissWhenActionHandle = NO;
                [alert addAction:action];
            }
            {
                @MK_WEAKIFY(alert);
                MKUIAlertAction *action = [MKUIAlertAction actionWithTitle:@"切换分隔线separatorViewBetweentTextAndActions" style:(MKUIAlertActionStyleDestructive) handler:^(MKUIAlertAction * _Nonnull action) {
                    @MK_NORMALIZE(alert);
                    alert.alertView.separatorViewBetweentTextAndActions = alert.alertView.separatorViewBetweentTextAndActions?nil:separatorViewBetweentTextAndActions;
                    [alert setNeedReloadData];
                }];
                action.shouldDismissWhenActionHandle = NO;
                [alert addAction:action];
            }
            MKUIListView *contentView = alert.alertView.contentView;
            contentView.groupBackgroundColor = UIColor.clearColor;
            contentView.groupBackgroundViewClass = nil;
            contentView.groupInsets = UIEdgeInsetsZero;
            contentView.sectionInteritemSpacing = 0;
            contentView.groupCornerRadius = 0;
            contentView.clipsToBounds = NO;
            contentView.backgroundView = [[TestMKUIAlertViewBackground alloc] init];
            
            alert.alertView.actionContentView.separatorStyle = MKUIListViewCellSeparatorStyleNone;
            
            UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
            textField.borderStyle = UITextBorderStyleRoundedRect;
            [alert.alertView.bodyContentView addItem:textField];
            
            [alert.alertView.bodyContentView setGroupInsets:UIEdgeInsetsMake(40, 16, 0, 16) forSection:0];
            [self presentViewController:alert animated:YES completion:nil];
        }];
        [self.listView addItem:el];
    }
    
    
    [self.listView addSection];
    {
        MKUIElementListViewCellButton *el = [MKUIElementListViewCellButton testButtonElementWithTitle:@"自定义Sheet" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
            @MK_NORMALIZE(self);
            TestUIAlertSheetView *sheet = [[TestUIAlertSheetView alloc] init];
            for(int i=0;i<2;i++){
                @MK_WEAKIFY(sheet);
                MKUIAlertAction *action = [MKUIAlertAction actionWithTitle:[NSString stringWithFormat:@"按钮%@",@(i)] style:(i%2==0?MKUIAlertActionStyleDestructive:MKUIAlertActionStyleDefault) handler:^(MKUIAlertAction * _Nonnull action) {
                    @MK_NORMALIZE(sheet);
                }];
                [sheet addAction:action];
            }
            {
                @MK_WEAKIFY(sheet);
                MKUIAlertAction *action = [MKUIAlertAction actionWithTitle:@"这是一个超级超级长的按钮/This is a very very long button" style:(MKUIAlertActionStyleDefault) handler:^(MKUIAlertAction * _Nonnull action) {
                    @MK_NORMALIZE(sheet);
                    @MK_WEAKIFY(sheet);
                    MKUIAlertAction *aciton = [MKUIAlertAction actionWithTitle:@"新增的按钮" style:MKUIAlertActionStyleDefault handler:^(MKUIAlertAction * _Nonnull action) {
                        @MK_NORMALIZE(sheet);
                        [sheet removeAction:action];
                        [sheet reloadData];
                    }];
                    aciton.shouldDismissWhenActionHandle = NO;
                    [sheet addAction:aciton];
                    
                    [sheet reloadData];
                }];
                action.shouldDismissWhenActionHandle = NO;
                [sheet addAction:action];
            }
            {
                @MK_WEAKIFY(sheet);
                MKUIAlertAction *action = [MKUIAlertAction actionWithTitle:@"取消" style:(MKUIAlertActionStyleCancel) handler:^(MKUIAlertAction * _Nonnull action) {
                    @MK_NORMALIZE(sheet);
                }];
                [sheet addAction:action];
            }
            [MKUIAlertWindow.sharedInstance showAlertView:sheet animated:YES completion:nil];
        }];
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellButton *el = [MKUIElementListViewCellButton testButtonElementWithTitle:@"添加右上角关闭按钮" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
            @MK_NORMALIZE(self);
            MKUIAlertController *alert = [self genAlertController];
            [alert.alertView sizeToFit];
            
            MKUIAutoLayoutViewContainer *buttonContainr = [[MKUIAutoLayoutViewContainer alloc] init];
            
            MKUIFlowLayoutButton *closeButton = [[MKUIFlowLayoutButton alloc] init];
            [closeButton setTitle:@"Close" forState:(UIControlStateNormal)];
            [closeButton setTitleColor:UIColor.mk_cancelStyleActionColor forState:(UIControlStateNormal)];
            [closeButton mk_addClickActionBlock:^(MKUIAlertController *alert) {
                [alert dismissViewControllerAnimated:YES completion:nil];
            } context:alert];
            [buttonContainr addSubview:closeButton];
            buttonContainr.whenLayoutSubviews = ^(__kindof UIView * _Nonnull buttonContainr) {
                MKUIFlowLayoutButton *closeButton = buttonContainr.subviews.firstObject;
                MKUIAlertView *alertView = [closeButton mk_firstSuperViewWithClass:MKUIAlertView.class];
                CGRect bounds = alertView.bounds;
                CGRect f1 = bounds;
                f1.size = [closeButton sizeThatFits:bounds.size];
                MKCGRectAlignMaxXToRect(&f1, bounds);
                MKCGRectAlignMinYToRect(&f1, bounds);
                buttonContainr.frame = f1;
                closeButton.frame = buttonContainr.bounds;
            };
            
            [alert.alertView addSubview:buttonContainr];
            [self presentViewController:alert animated:YES completion:nil];
        }];
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellButton *el = [[MKUIElementListViewCellButton alloc] init];
        el.elementView.titleText = @"AlertToView";
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
            @MK_NORMALIZE(self);
            [self showAlertToView];
        };
        [self.listView addItem:el];
    }
    [self.listView reloadData];
}
- (void)showAlertToView{
    MKUIAlertView *alert = [self genAlertView];
    [alert presentToView:self.view animated:YES completion:^(BOOL finished) {
        NSLog(@"alert completion");
    }];
}
- (MKUIAlertController *)genAlertController{
    TestMKUIAlertViewParam *paramVC = self.tabBarController.viewControllers[1];
    [paramVC view];
    MKUIAlertController *alert = [paramVC genAlertController];
    return alert;
}
- (MKUIAlertView *)genAlertView{
    TestMKUIAlertViewParam *paramVC = self.tabBarController.viewControllers[1];
    [paramVC view];
    MKUIAlertView *alert = [paramVC genAlertView];
    return alert;
}
@end

@implementation TestMKUIAlertViewParam
- (id)init{
    if(self=[super init]){
        self.title = @"MKUIAlert";
        self.mk_customTabBarItem.image = Menu.randomImage;
    }
    return self;
}
- (void)viewDidLoad{
    [super viewDidLoad];
    //
    self.listView.style = MKUIListViewStyleInsetGrouped;
    self.alertPreferredStyle = [MKUITestCache.sharedStorage mk_integerForKeyPath:@"TestMKUIAlertViewParam.alertPreferredStyle"];
    self.alertTitle = [MKUITestCache.sharedStorage mk_stringForKeyPath:@"TestMKUIAlertViewParam.alertTitle"];
    self.alertMessage = [MKUITestCache.sharedStorage mk_stringForKeyPath:@"TestMKUIAlertViewParam.alertMessage"];
    self.alertDuration = [MKUITestCache.sharedStorage mk_CGFloatForKeyPath:@"TestMKUIAlertViewParam.alertDuration"];
    self.alertActions = [[NSMutableArray alloc] initWithArray:[[MKUITestCache.sharedStorage mk_arrayForKeyPath:@"TestMKUIAlertViewParam.alertActions"] mk_map:^id _Nullable(NSDictionary *obj) {
        return [MKUIAlertAction actionWithTestJsonValue:obj];
    }]];
    self.enableActions = [MKUITestCache.sharedStorage mk_boolForKeyPath:@"TestMKUIAlertViewParam.enableActions" otherwise:YES];
    self.showMaskView = [MKUITestCache.sharedStorage mk_boolForKeyPath:@"TestMKUIAlertViewParam.showMaskView" otherwise:YES];
    self.alertInShareWindow = [MKUITestCache.sharedStorage mk_boolForKeyPath:@"TestMKUIAlertViewParam.alertInShareWindow"];
    self.maxHeight = [MKUITestCache.sharedStorage mk_CGFloatForKeyPath:@"TestMKUIAlertViewParam.maxHeight"];
    [self reloadData];
}
- (void)reloadData{
    @MK_WEAKIFY(self);
    [self.listView deleteAllSections];
    [self.listView addSection];
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testElementMKUIAlertViewStyle:@"preferredStyle" selected:self.alertPreferredStyle whenDone:^(MKUIAlertViewStyle selectedValue) {
            @MK_NORMALIZE(self);
            self.alertPreferredStyle = selectedValue;
            [MKUITestCache.sharedStorage setObject:@(selectedValue) forKey:@"TestMKUIAlertViewParam.alertPreferredStyle"];
        }];
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSStringElementWithTitle:@"Title" selected:self.alertTitle whenDone:^(NSString * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            self.alertTitle = selectedValue;
            [MKUITestCache.sharedStorage setObject:selectedValue forKey:@"TestMKUIAlertViewParam.alertTitle"];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSStringElementWithTitle:@"Message" selected:self.alertMessage whenDone:^(NSString * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            self.alertMessage = selectedValue;
            [MKUITestCache.sharedStorage setObject:selectedValue forKey:@"TestMKUIAlertViewParam.alertMessage"];
        }];
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSIntegerElementWithTitle:@"延时Alert" selected:self.alertDuration whenDone:^(NSInteger selectedValue) {
            @MK_NORMALIZE(self);
            self.alertDuration = selectedValue;
            [MKUITestCache.sharedStorage setObject:@(selectedValue) forKey:@"TestMKUIAlertViewParam.alertDuration"];
        }];
        el.elementView.subTitleText = @"单位秒";
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"在独立Window中展示" on:self.enableActions whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            self.alertInShareWindow = on;
            [MKUITestCache.sharedStorage setObject:@(on) forKey:@"TestMKUIAlertViewParam.alertInShareWindow"];
        }];
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"enableActions" on:self.enableActions whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            self.enableActions = on;
            [MKUITestCache.sharedStorage setObject:@(on) forKey:@"TestMKUIAlertViewParam.enableActions"];
        }];
        [self.listView addItem:el];
    }
    {
        for(MKUIAlertAction *action in self.alertActions){
            TestMKUIElementListViewCellMKUIAlertAction *el = [[TestMKUIElementListViewCellMKUIAlertAction alloc] init];
            el.elementView.alertAction = action;
            el.elementView.whenDone = ^(__kindof TestMKUIElementViewMKUIAlertAction * _Nonnull el) {
                @MK_NORMALIZE(self);
                NSIndexPath *path = [self.listView indexPathForCell:el.mk_nearestAttributeViewInListView];
                self.alertActions[path.item-1] = el.alertAction;
                [self _saveAlertActions];
            };
            el.elementView.accessView = [el.elementView deleteButtonWithAction:^(TestMKUIElementViewMKUIAlertAction * _Nonnull el) {
                @MK_NORMALIZE(self);
                NSIndexPath *path = [self.listView indexPathForCell:el.mk_nearestAttributeViewInListView];
                [self.alertActions removeObjectAtIndex:path.item-1];
                [self _saveAlertActions];
            }];
            [self.listView addItem:el];
        }
    }
    {
        NSInteger section = self.listView.numberOfSections-1;
        MKUIElementListViewCellButton *el = [[MKUIElementListViewCellButton alloc] init];
        el.elementView.titleText = @"添加按钮";
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el0) {
            @MK_NORMALIZE(self);
            @MK_WEAKIFY(self);
            TestMKUIElementListViewCellMKUIAlertAction *el = [[TestMKUIElementListViewCellMKUIAlertAction alloc] init];
            el.elementView.alertAction = [MKUIAlertAction actionWithTitle:@"按钮标题" style:(MKUIAlertActionStyleDefault) handler:nil];
            el.elementView.whenDone = ^(__kindof TestMKUIElementViewMKUIAlertAction * _Nonnull el) {
                @MK_NORMALIZE(self);
                NSIndexPath *path = [self.listView indexPathForCell:el.mk_nearestAttributeViewInListView];
                self.alertActions[path.item-1] = el.alertAction;
                [self _saveAlertActions];
            };
            el.elementView.accessView = [el.elementView deleteButtonWithAction:^(TestMKUIElementViewMKUIAlertAction * _Nonnull el) {
                @MK_NORMALIZE(self);
                NSIndexPath *path = [self.listView indexPathForCell:el.mk_nearestAttributeViewInListView];
                [self.alertActions removeObjectAtIndex:path.item-1];
                [self _saveAlertActions];
            }];
            [self.listView insertItem:el atIndexPath:[NSIndexPath indexPathForRow:1 inSection:section] withAnimation:(MKUIListViewItemAnimationAutomatic)];
            [self.alertActions insertObject:el.elementView.alertAction atIndex:0];
            [self _saveAlertActions];
        };
        [self.listView addItem:el];
        
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider testSliderElementWithTitle:@"设置maxHeight" value:self.maxHeight minimumValue:0 maximumValue:400 whenChange:^(TestMKUIElementViewSlider * _Nonnull el, CGFloat value) {
            @MK_NORMALIZE(self);
            [MKUITestCache.sharedStorage setObject:@(value) forKey:@"TestMKUIAlertViewParam.maxHeight"];
            self.maxHeight = value;
        }]];
    }
    
    
    [self.listView addSection];
    {
        MKUIElementListViewCellButton *el = [[MKUIElementListViewCellButton alloc] init];
        el.elementView.titleText = @"Alert";
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
            @MK_NORMALIZE(self);
            if(self.alertDuration<=0){
                [self showAlert];
            }else{
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.alertDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self showAlert];
                });
            }
        };
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"showMaskView" on:self.showMaskView whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            self.showMaskView = on;
            [MKUITestCache.sharedStorage setObject:@(on) forKey:@"TestMKUIAlertViewParam.showMaskView"];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellButton *el = [[MKUIElementListViewCellButton alloc] init];
        el.elementView.titleText = @"AlertToView";
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
            @MK_NORMALIZE(self);
            if(self.alertDuration<=0){
                [self showAlertToView];
            }else{
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.alertDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self showAlertToView];
                });
            }
        };
        [self.listView addItem:el];
    }
    [self.listView reloadData];
}
- (MKUIAlertController *)genAlertController{
    MKUIAlertController *alert = [MKUIAlertController alertControllerWithTitle:self.alertTitle message:self.alertMessage preferredStyle:(self.alertPreferredStyle)];
    alert.alertView.showMaskView = self.showMaskView;
    alert.alertView.maxHeight = self.maxHeight;
    if(self.enableActions){
        for(MKUIAlertAction *action0 in self.alertActions){
            MKUIAlertAction *action = [action0 copy];
            action.handler = ^(MKUIAlertAction * _Nonnull action) {
                NSLog(@"click action:%@",action.testDescription);
            };
            [alert addAction:action];
        }
    }
    return alert;
}
- (MKUIAlertView *)genAlertView{
    MKUIAlertView *alert = [MKUIAlertView alertViewWithTitle:self.alertTitle message:self.alertMessage preferredStyle:(self.alertPreferredStyle)];
    alert.showMaskView = self.showMaskView;
    if(self.enableActions){
        for(MKUIAlertAction *action0 in self.alertActions){
            MKUIAlertAction *action = [action0 copy];
            action.handler = ^(MKUIAlertAction * _Nonnull action) {
                NSLog(@"click action:%@",action.testDescription);
            };
            [alert addAction:action];
        }
    }
    return alert;
}
- (void)showAlert{
    MKUIAlertController *alert = [self genAlertController];
    //UIAlertActionStyleCancel只能有一个,列表展示时，cancel位于底部或最左侧
    //系统一次只能alert一个
    if(self.alertInShareWindow){
        [alert presentInSharedWindowWithAnimated:YES completion:nil];
    }else{
        [self presentViewController:alert animated:YES completion:nil];
    }
}
- (void)showAlertToView{
    MKUIAlertView *alert = [self genAlertView];
    [alert presentToView:self.view animated:YES completion:^(BOOL finished) {
        NSLog(@"alert completion");
    }];
}
- (void)_saveAlertActions{
    [MKUITestCache.sharedStorage setObject:[self.alertActions mk_map:^id _Nullable(MKUIAlertAction *obj) {
        return obj.testJsonValue;
    }] forKey:@"TestMKUIAlertViewParam.alertActions"];
}
@end

@implementation TestUIAlertControllerParam
- (id)init{
    if(self=[super init]){
        self.title = @"系统Alert";
        self.mk_customTabBarItem.image = Menu.randomImage;
    }
    return self;
}
- (void)viewDidLoad{
    [super viewDidLoad];
    self.alertActions = [[NSMutableArray alloc] init];
    //
    self.listView.style = MKUIListViewStyleInsetGrouped;
    self.alertPreferredStyle = [MKUITestCache.sharedStorage mk_integerForKeyPath:@"TestUIAlertControllerParam.alertPreferredStyle"];
    self.alertTitle = [MKUITestCache.sharedStorage mk_stringForKeyPath:@"TestUIAlertControllerParam.alertTitle"];
    self.alertMessage = [MKUITestCache.sharedStorage mk_stringForKeyPath:@"TestUIAlertControllerParam.alertMessage"];
    self.alertDuration = [MKUITestCache.sharedStorage mk_CGFloatForKeyPath:@"TestUIAlertControllerParam.alertDuration"];
    self.alertActions = [[NSMutableArray alloc] initWithArray:[[MKUITestCache.sharedStorage mk_arrayForKeyPath:@"TestUIAlertControllerParam.alertActions"] mk_map:^id _Nullable(NSDictionary *obj) {
        return [UIAlertAction actionWithTestJsonValue:obj];
    }]];
    self.enableActions = [MKUITestCache.sharedStorage mk_boolForKeyPath:@"TestUIAlertControllerParam.enableActions"];
    [self reloadData];
}
- (void)reloadData{
    @MK_WEAKIFY(self);
    [self.listView deleteAllSections];
    [self.listView addSection];
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testElementUIAlertControllerStyle:@"preferredStyle" selected:self.alertPreferredStyle whenDone:^(UIAlertControllerStyle selectedValue) {
            @MK_NORMALIZE(self);
            self.alertPreferredStyle = selectedValue;
            [MKUITestCache.sharedStorage setObject:@(selectedValue) forKey:@"TestUIAlertControllerParam.alertPreferredStyle"];
        }];
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSStringElementWithTitle:@"Title" selected:self.alertTitle whenDone:^(NSString * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            self.alertTitle = selectedValue;
            [MKUITestCache.sharedStorage setObject:selectedValue forKey:@"TestUIAlertControllerParam.alertTitle"];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSStringElementWithTitle:@"Message" selected:self.alertMessage whenDone:^(NSString * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            self.alertMessage = selectedValue;
            [MKUITestCache.sharedStorage setObject:selectedValue forKey:@"TestUIAlertControllerParam.alertMessage"];
        }];
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSIntegerElementWithTitle:@"延时Alert" selected:self.alertDuration whenDone:^(NSInteger selectedValue) {
            @MK_NORMALIZE(self);
            self.alertDuration = selectedValue;
            [MKUITestCache.sharedStorage setObject:@(selectedValue) forKey:@"TestUIAlertControllerParam.alertDuration"];
        }];
        el.elementView.subTitleText = @"单位秒";
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"enableActions" on:self.enableActions whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            self.enableActions = on;
            [MKUITestCache.sharedStorage setObject:@(on) forKey:@"TestUIAlertControllerParam.enableActions"];
        }];
        [self.listView addItem:el];
    }
    {
        for(UIAlertAction *action in self.alertActions){
            TestMKUIElementListViewCellUIAlertAction *el = [[TestMKUIElementListViewCellUIAlertAction alloc] init];
            el.elementView.alertAction = action;
            el.elementView.whenDone = ^(__kindof TestMKUIElementViewUIAlertAction * _Nonnull el) {
                @MK_NORMALIZE(self);
                NSIndexPath *path = [self.listView indexPathForCell:el.mk_nearestAttributeViewInListView];
                self.alertActions[path.item-1] = el.alertAction;
                [self _saveAlertActions];
            };
            el.elementView.accessView = [el.elementView deleteButtonWithAction:^(TestMKUIElementViewUIAlertAction * _Nonnull el) {
                @MK_NORMALIZE(self);
                NSIndexPath *path = [self.listView indexPathForCell:el.mk_nearestAttributeViewInListView];
                [self.alertActions removeObjectAtIndex:path.item-1];
                [self _saveAlertActions];
            }];
            [self.listView addItem:el];
        }
    }
    {
        NSInteger section = self.listView.numberOfSections-1;
        MKUIElementListViewCellButton *el = [[MKUIElementListViewCellButton alloc] init];
        el.elementView.titleText = @"添加按钮";
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el0) {
            @MK_NORMALIZE(self);
            @MK_WEAKIFY(self);
            TestMKUIElementListViewCellUIAlertAction *el = [[TestMKUIElementListViewCellUIAlertAction alloc] init];
            el.elementView.alertAction = [UIAlertAction actionWithTitle:@"按钮标题" style:(UIAlertActionStyleDefault) handler:nil];
            el.elementView.whenDone = ^(__kindof TestMKUIElementViewUIAlertAction * _Nonnull el) {
                @MK_NORMALIZE(self);
                NSIndexPath *path = [self.listView indexPathForCell:el.mk_nearestAttributeViewInListView];
                self.alertActions[path.item-1] = el.alertAction;
                [self _saveAlertActions];
            };
            el.elementView.accessView = [el.elementView deleteButtonWithAction:^(TestMKUIElementViewUIAlertAction * _Nonnull el) {
                @MK_NORMALIZE(self);
                NSIndexPath *path = [self.listView indexPathForCell:el.mk_nearestAttributeViewInListView];
                [self.alertActions removeObjectAtIndex:path.item-1];
                [self _saveAlertActions];
            }];
            [self.listView insertItem:el atIndexPath:[NSIndexPath indexPathForRow:1 inSection:section] withAnimation:(MKUIListViewItemAnimationAutomatic)];
            [self.alertActions insertObject:el.elementView.alertAction atIndex:0];
            [self _saveAlertActions];
        };
        [self.listView addItem:el];
        
    }
    
    
    [self.listView addSection];
    {
        MKUIElementListViewCellButton *el = [[MKUIElementListViewCellButton alloc] init];
        el.elementView.titleText = @"Alert";
        el.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
            @MK_NORMALIZE(self);
            if(self.alertDuration<=0){
                [self showAlert];
            }else{
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.alertDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self showAlert];
                });
            }
        };
        [self.listView addItem:el];
    }
    [self.listView reloadData];
}
- (void)showAlert{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:self.alertTitle message:self.alertMessage preferredStyle:(self.alertPreferredStyle)];
    
    if(self.enableActions){
        for(UIAlertAction *action in self.alertActions){
            UIAlertAction *action0 = [UIAlertAction actionWithTitle:action.title style:action.style handler:^(UIAlertAction * _Nonnull action) {
                NSLog(@"click action:%@",action.testDescription);
            }];
            action0.enabled = action.isEnabled;
            [alert addAction:action0];
        }
    }
    //UIAlertActionStyleCancel只能有一个,列表展示时，cancel位于底部或最左侧
    if(self.alertPreferredStyle==UIAlertControllerStyleAlert){
//        [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
//            
//        }];
//        [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
//            
//        }];
    }
    //系统一次只能alert一个
    [self presentViewController:alert animated:YES completion:nil];
}
- (void)_saveAlertActions{
    [MKUITestCache.sharedStorage setObject:[self.alertActions mk_map:^id _Nullable(UIAlertAction *obj) {
        return obj.testJsonValue;
    }] forKey:@"TestUIAlertControllerParam.alertActions"];
}
@end

@interface TestAlertViewController ()

@end

@implementation TestAlertViewController
+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"Alert弹窗" viewControllerClass:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.mk_listViewBackgroundColor;
    self.viewControllers = @[
        [TestUIAlertControllerParam new],
        [TestMKUIAlertViewParam new],
        [TestMultipAlertParam new],
    ];
    self.selectedIndex = 0;
}
@end
